<template>
	<view class="content">
		<view class="info_div">

			<view class="item_div">
				<view style="display: flex;">
					<view style="color: red;">*</view>公司名称 |
				</view>
				<input class="et" placeholder="请输入公司名" @input="onInputCompanyName" :value="info.company_name" />
			</view>

			<view class="item_div">
				<view style="display: flex;">
					<view style="color: red;">*</view>
					<view style="visibility: hidden;">空</view>联系人 |
				</view>
				<input class="et" placeholder="请输入姓名" @input="onInputName" :value="info.name" />
			</view>

			<view class="item_div">
				<view style="display: flex;">
					<view style="color: red;">*</view>服务电话 |
				</view>
				<input class="et" placeholder="请输入电话" @input="onInputPhone" :value="info.servicephone" />
			</view>

			<view class="item_div_2">
				<view style="display: flex;">
					<view style="color: red;">*</view>公司地址 |
				</view>
				<textarea class="et_2" placeholder="请输入详细地址" @input="onInputAddr" :value="info.address" />
			</view>

			<view class="item_div" style="display: none;">
				<view style="display: flex;">
					<view style="color: red;">*</view>地理位置 |
				</view>
				<view class="et" @click.stop="onChooseLocation">{{map_position}} ></view>
			</view>

		</view>

		<view class="btn_ok" @click.stop="onOk">确定</view>

	</view>
</template>

<script>
	import { getUInfo } from '@/util/util.js'
	export default {
		data() {
			return {
				info: {
					company_name: '',
					name: '',
					servicephone: '',
					address: '',
					latitude: 35.103628,
					longitude: 118.356410,
				},
				area: ['山东省', '临沂市', '兰山区'],

				map_position: '临沂市', // 地图选点位置
				location: {}, // 地理位置对象
			}
		},

		onLoad(opts) {
			if (opts.data) {
				let data = JSON.parse(opts.data)
				this.info = data
			}
		},

		methods: {

			// 打开地图选点
			onChooseLocation() {
				uni.chooseLocation({
					success: (res) => {
						console.log('地图选择位置的结果 = ', JSON.stringify(res));
						this.map_position = res.name
						this.location = res
						this.info.longitude = res.longitude
						this.info.latitude = res.latitude
					},
					fail: (res) => {
						console.log('onChooseLocation.fail = ', JSON.stringify(res));
						uni.showToast({ title: '未获取到位置信息', icon: 'none' })
					}
				})

			},

			// 输入物流名
			onInputCompanyName(e) {
				this.info.company_name = e.detail.value
			},

			// 输入电话
			onInputPhone(e) {
				this.info.mobile = e.detail.value
			},

			// 输入姓名
			onInputName(e) {
				this.info.name = e.detail.value
			},

			// 输入详细地址
			onInputAddr(e) {
				this.info.address = e.detail.value
			},

			onchange(e) {
				console.log('e.detail.value = ', e.detail.value);
			},

			onnodeclick(node) {
				console.log('onnodeclick = ', node);
			},

			// 确认添加
			onOk(flag) {
				let that = this
				if (flag == -1) {
					uni.showToast({ icon: 'none', title: '请使用小程序添加地址' })
					return
				}
				if (!this.info.company_name) {
					uni.showToast({ icon: 'none', title: '请输入公司名称' })
					return
				}
				if (!this.info.name) {
					uni.showToast({ icon: 'none', title: '请输入联系人' })
					return
				}
				if (!this.info.servicephone) {
					uni.showToast({ icon: 'none', title: '请输入服务电话' })
					return
				}

				if (!this.info.address) {
					uni.showToast({ icon: 'none', title: '请输入公司地址' })
					return
				}

				uni.showModal({
					title: '提示',
					content: '是否提交信息？',
					success: (res) => {
						res.confirm && that.postData()
					}
				})
			},

			// 接口:提交数据
			async postData() {
				let that = this
				let token = getUInfo().token
				uni.showLoading({ title: '处理中', mask: true, })
				let res = await that.$editCom({ token, ...this.info })
				if (res.code == 1) {
					uni.$emit('onEditLogInfo', { msg: 'onEditLogInfo' })
					uni.showModal({ title: '提示', content: `${res.msg}`, showCancel: false, success: (res) => { res.confirm && uni.navigateBack() } })
				} else {
					uni.showToast({ title: res.msg, icon: 'none' })
				}
			},

		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	/* 分割线 */
	.divider {
		width: 100%;
		height: 1rpx;
		background-color: rgba(0, 0, 0, 0.2);
	}

	.info_div {
		display: flex;
		flex-direction: column;
		width: calc(100% - 40rpx);
		margin: 20rpx 20rpx 0 20rpx;
	}

	/* 每个填写项目的容器 */
	.item_div {
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 60rpx;
		line-height: 60rpx;
		margin: 22rpx 0 0 0;
		font-size: 28rpx;
		/* background-color: red; */
		overflow: hidden;
	}

	/* 大一点的项目容器 */
	.item_div_2 {
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 100rpx;
		/* line-height: 100rpx; */
		margin: 30rpx 0 0 0;
		font-size: 28rpx;
	}

	/* 预约时间项目的容器 */
	.item_div_3 {
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 86rpx;
		line-height: 60rpx;
		margin: 22rpx 0 0 0;
		font-size: 28rpx;
		/* background-color: red; */
		overflow: hidden;
	}

	.et {
		/* background-color: red; */
		flex-grow: 1;
		margin: 0 0 0 0rpx;
		max-width: 70%;
		height: 60rpx;
		text-align: right;
		font-size: 28rpx;
		color: #666;
	}

	.et_2 {
		/* background-color: red; */
		flex-grow: 1;
		margin: 0 0 0 0rpx;
		max-width: 70%;
		height: 100rpx;
		/* line-height: 100rpx; */
		text-align: right;
		font-size: 28rpx;
		color: #666;
		/* border: solid 1rpx gray; */
		/* vertical-align: middle; */
	}

	.et_3 {
		display: flex;
		/* background-color: red; */
		flex-grow: 1;
		margin: 0 0 0 0rpx;
		max-width: 70%;
		height: 60rpx;
		font-size: 28rpx;
		color: #666;
		/* background-color: red; */
	}

	.picker_region {
		width: 70%;
		height: 100%;
		text-align: right;
		/* background: red; */
	}

	.btn_ok {
		align-self: center;
		background: #F7D34B;
		width: 90%;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 30rpx;
		font-weight: bold;
		margin: 88rpx 0 0 0;
		border-radius: 12rpx;
	}
</style>